package com.example.whatrubbish.controller;

import com.alibaba.fastjson.JSONArray;
import com.example.whatrubbish.model.Feedback;
import com.example.whatrubbish.service.FeedbackService;
import com.example.whatrubbish.utils.Constants;
import com.example.whatrubbish.utils.JsonResult;
import com.example.whatrubbish.vo.Criteria;
import com.example.whatrubbish.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("feedback")
public class FeedbackController {
    @Autowired
    FeedbackService feedbackService;

    @RequestMapping("/insert")
    public JsonResult insert(Feedback feedback){
        JsonResult jsonResult;
        try{
            String regForEmail = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
            if (!feedback.getFeedbackEmail().matches(regForEmail))
                return new JsonResult(Constants.STATUS_NOT_FOUND, "邮箱格式不正确哟！");
            String regForPhone = "^[1][3-9][0-9]{9}$";
            if (!feedback.getFeedbackPhone().matches(regForPhone))
                return new JsonResult(Constants.STATUS_NOT_FOUND, "手机号码格式不正确哟！");
            int i = feedbackService.insertSelective(feedback);
            if(i != 0){
                jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success", i);
            }else{
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "fail", i);
            }
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "error");
        }
        return jsonResult;
    }

    /**
     * 查询未读
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @RequestMapping("/selectUnRead")
    public JsonResult selectUnRead(@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        JsonResult jsonResult;
        try {
            Criteria criteria = new Criteria();
            criteria.addExtField("pageIndex", (pageIndex-1) * pageSize);
            criteria.addExtField("pageSize", pageSize);
            PageBean<Feedback> pageBean = feedbackService.selectAllUnRead(criteria);
            pageBean.setPageIndex(pageIndex);
            pageBean.setPageSize(pageSize);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success", pageBean);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "fail");
        }
        return jsonResult;
    }

    /**
     * 查询已读
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @RequestMapping("/selectRead")
    public JsonResult selectRead(@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        JsonResult jsonResult;
        try {
            Criteria criteria = new Criteria();
            criteria.addExtField("pageIndex", (pageIndex-1) * pageSize);
            criteria.addExtField("pageSize", pageSize);
            PageBean<Feedback> pageBean = feedbackService.selectAllRead(criteria);
            pageBean.setPageIndex(pageIndex);
            pageBean.setPageSize(pageSize);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success", pageBean);
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "fail");
        }
        return jsonResult;
    }

    /**
     * 查询已删除
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @RequestMapping("/selectDelete")
    public JsonResult selectDelete(@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        JsonResult jsonResult;
        try {
            Criteria criteria = new Criteria();
            criteria.addExtField("pageIndex", (pageIndex-1) * pageSize);
            criteria.addExtField("pageSize", pageSize);
            PageBean<Feedback> pageBean = feedbackService.selectAllDelete(criteria);
            pageBean.setPageIndex(pageIndex);
            pageBean.setPageSize(pageSize);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success", pageBean);
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "fail");
        }
        return jsonResult;
    }

    /**
     * 标为已读
     * @param feedbackId
     * @return
     */
    @RequestMapping("/updateReadById")
    public JsonResult updateReadById(@RequestParam(value = "feedbackId") Long feedbackId){
        JsonResult jsonResult;
        try {
            int i = feedbackService.updateReadById(feedbackId);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "error");
        }
        return jsonResult;
    }

    /**
     * 逻辑删除
     * @param feedbackId
     * @return
     */
    @RequestMapping("/deleteById")
    public JsonResult deleteById(@RequestParam(value = "feedbackId") Long feedbackId){
        JsonResult jsonResult;
        try {
            int i = feedbackService.deleteById(feedbackId);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "error");
        }
        return jsonResult;
    }

    /**
     * 物理删除
     * @param feedbackId
     * @return
     */
    @RequestMapping("/deleteByPrimaryKey")
    public JsonResult deleteByPrimaryKey(@RequestParam(value = "feedbackId") Long feedbackId){
        JsonResult jsonResult;
        try {
            int i = feedbackService.deleteByPrimaryKey(feedbackId);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "error");
        }
        return jsonResult;
    }

    /**
     * 标为已读(批量)
     * @param map
     * @return
     */
    @RequestMapping("/updateReadByIdList")
    public JsonResult updateReadByIdList(@RequestBody Map map){
        JsonResult jsonResult;
        try{
            List<Feedback> readList = JSONArray.parseArray(map.get("readList").toString(), Feedback.class);
            int i = feedbackService.updateReadByList(readList);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success", i);
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "fail");
        }
        return jsonResult;
    }

    /**
     * 逻辑删除(批量)
     * @param map
     * @return
     */
    @RequestMapping("/deleteByList")
    public JsonResult deleteByList(@RequestBody Map map){
        JsonResult jsonResult;
        try{
            List<Feedback> deleteList = JSONArray.parseArray(map.get("deleteList").toString(), Feedback.class);
            int i = feedbackService.deleteByList(deleteList);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success", i);
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "fail");
        }
        return jsonResult;
    }

    /**
     * 物理删除(批量)
     * @param map
     * @return
     */
    @RequestMapping("/deleteByPrimaryKeyList")
    public JsonResult deleteByPrimaryKeyList(@RequestBody Map map){
        JsonResult jsonResult;
        try{
            List<Feedback> deleteList = JSONArray.parseArray(map.get("deleteList").toString(), Feedback.class);
            int i = feedbackService.deleteByPrimaryKeyList(deleteList);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success", i);
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "fail");
        }
        return jsonResult;
    }

}
